package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.pojo.ShljElectricityMeter;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guofen.service.shlj.StorageBoxCleanService;
import com.guofen.dao.ShljStorageBoxCleanMapper;
import com.guofen.pojo.ShljStorageBoxClean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StorageBoxCleanServiceImpl implements StorageBoxCleanService{
    @Autowired
    private ShljStorageBoxCleanMapper shljStorageBoxCleanMapper;

    @Override
    public boolean add(String wagonNumber,
                       String arrivalTime,
                       String leaveTime,
                       Double cleanWeight,
                       Double residueWeight,
                       String cleanType,
                       String residueType,
                       String recordingPerson,
                       String remarks){
        String date = DateAndTimeUtil.getDate();

        ShljStorageBoxClean shljStorageBoxClean = new ShljStorageBoxClean();
        shljStorageBoxClean.setDate(date);
        shljStorageBoxClean.setWagonNumber(wagonNumber);
        shljStorageBoxClean.setArrivalTime(arrivalTime);
        shljStorageBoxClean.setLeaveTime(leaveTime);
        shljStorageBoxClean.setCleanWeight(cleanWeight);
        shljStorageBoxClean.setCleanType(cleanType);
        shljStorageBoxClean.setResidueWeight(residueWeight);
        shljStorageBoxClean.setResidueType(residueType);
        shljStorageBoxClean.setRecordingPerson(recordingPerson);
        shljStorageBoxClean.setRemarks(remarks);

        int result = shljStorageBoxCleanMapper.insert(shljStorageBoxClean);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljStorageBoxClean> list = shljStorageBoxCleanMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljStorageBoxClean> list = shljStorageBoxCleanMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljStorageBoxCleanMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljStorageBoxClean record) {
        return shljStorageBoxCleanMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ShljStorageBoxClean getlast(){
        return shljStorageBoxCleanMapper.getLast();
    }
}